/**
 * 角色管理的单例
 */
var Role = {
    id: "roleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};
/**
 * 操作
 */
Role.editFormatter=function(value,row,index){
    var btnArr =[];
    var btnUpdate =  {clickEvent:'Role.openChangeRole('+row.id+');',title:'修改角色',icon:HsetIcon['update'],active:false};
    var btnDelete =  {clickEvent:'Role.delRole('+row.id+',\''+row.name+'\');',title:'删除角色',icon:HsetIcon['delete'],active:false};
    var btnAssign =  {clickEvent:'Role.assign('+row.id+');',title:'权限配置',icon:HsetIcon['assign'],active:false};
    if(Authority['update']==true){
        btnUpdate.active=true;
    }
    if(Authority['delete']==true){
        btnDelete.active=true;
    }
    if(Authority['assign']==true){
        btnAssign.active=true;
    }
    btnArr.push(btnUpdate);
    btnArr.push(btnDelete);
    btnArr.push(btnAssign);
    return HsetIcon.createGroup(btnArr);
};
/**
 * 初始化表格的列
 */
Role.initColumn = function () {
    var columns = [
        {title: '序号',align : 'center',valign : 'middle',width:20,radio: false,checkbox:false,formatter:function (value, row, index) {  return index+1; } },
       // {title: 'id', field: 'id', visible: false, width:20,align: 'center', valign: 'middle'},
        {title: '名称', field: 'name', align: 'center', width:200,valign: 'middle', sortable: true},
        {title: '上级角色', field: 'pName', align: 'center', width:200, valign: 'middle', sortable: true},
        {title: '别名', field: 'tips', align: 'center', width:200, valign: 'middle', sortable: true},
        {title: '操作', align: 'center', valign: 'middle', width:140, formatter:Role.editFormatter},
    ]
    return columns;
};

/**
 * 点击添加角色
 */
Role.openAddRole = function () {
    var index = layer.open({
        type: 2,
        title: '添加角色',
        shade: [0.5,'FFFFFF'],
        area: ['950px', '240px'], //宽高
        fix: false, //不固定
        maxmin: false,
        offset: '10px', //定位高度   如需定位top、left; offset:  ['100px', '200px']
        content: Feng.ctxPath + '/role/role_add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改按钮时
 */
Role.openChangeRole = function (id) {
        var index = layer.open({
            type: 2,
            title: '修改角色',
            shade: [0.5,'FFFFFF'],
            area: ['950px', '240px'], //宽高
            fix: false, //不固定
            maxmin: false,
            offset: '10px', //定位高度   如需定位top、left; offset:  ['100px', '200px']
            content: Feng.ctxPath + '/role/role_edit/' + id
        });
        this.layerIndex = index;
};

/**
 * 删除角色
 */
Role.delRole = function (id,name) {
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/role/remove", function () {
                Feng.success("删除成功!");
                Role.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("roleId", id);
            ajax.setZLoadingIn(true);
        	ajax.setZLoadingOut(true);
            ajax.start();
        };
        Feng.confirm("是否删除角色 " + name+ "?",operation);
};

/**
 * 权限配置
 */
Role.assign = function (id) {
        var index = layer.open({
            type: 2,
            title: '权限配置',
            shade: [0.5,'FFFFFF'],
            area: ['500px', '600px'], //宽高
            fix: false, //不固定
            maxmin: false,
            offset: '120px', //定位高度   如需定位top、left; offset:  ['100px', '200px']
            content: Feng.ctxPath + '/role/role_assign/' + id
        });
        this.layerIndex = index;
};
/**
 * 自定义按钮组
 */
Role.btnGroup=function(){
    var arr=[];
    if(Authority['add']==true){//添加角色
        arr.push({clickEvent:'Role.openAddRole();',title:'添加角色',icon:HsetIcon['add']});
    }
    return arr;
};

$(function () {

    var defaultColunms = Role.initColumn();
    var table = new BSTable(Role.id, "/role/list", defaultColunms);
    table.setPaginationType("client");
    table.setBtnGroup(Role.btnGroup());
    Role.table=table.init();
    Role.vs= new $vs(Role.table).init();
});
